package com.bifang.module.file.core.biz.impl;

import cn.hutool.core.convert.Convert;
import com.bifang.engine.assistant.core.annotation.ApiService;
import com.bifang.engine.assistant.core.annotation.OpApi;
import com.bifang.engine.assistant.core.annotation.SaveOplog;
import com.bifang.engine.assistant.core.enums.AuTypeEnum;
import com.bifang.engine.assistant.core.enums.FuncTypeEnum;
import com.bifang.engine.assistant.core.exception.AppException;
import com.bifang.module.file.common.vo.FileInfoVO;
import com.bifang.module.file.common.vo.FileKeyVO;
import com.bifang.module.file.common.vo.FileMd5VO;
import com.bifang.module.file.common.vo.FileMetadataVO;
import com.bifang.module.file.core.biz.FileInfoIntegration;
import com.bifang.module.file.core.biz.FileManagerIntegration;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@ApiService(funcCode = "fileManager", title = "文件信息管理")
public class FileManagerIntegrationImpl implements FileManagerIntegration {
    @Autowired
    private FileInfoIntegration fileInfoIntegration;

    @Override
    @OpApi(funcCode = "fileManager0001", title = "获取文件信息列表", funcType = FuncTypeEnum.query)
    public PageInfo<FileInfoVO> getFileInfoList(FileInfoVO fileInfoVO) {
        Map<String,Object> map = Convert.toMap(String.class,Object.class,fileInfoVO);
        map.forEach((key,val)->{
            if(val instanceof String){
                if(StringUtils.isBlank((String)val)){
                    map.put(key,null);
                }
            }
        });
        fileInfoVO = Convert.convert(FileInfoVO.class,map);
        return fileInfoIntegration.getFileInfoList(fileInfoVO);
    }

    @Override
    @OpApi(funcCode = "fileManager0002", title = "获取文件详细信息", funcType = FuncTypeEnum.query)
    public FileInfoVO getFileInfoDetail(FileKeyVO fileKeyVO) {
        FileInfoVO infoVO;
        if(fileKeyVO.getFileId()!=null){
            infoVO = fileInfoIntegration.getFileInfoDetail(fileKeyVO.getFileId());
        }else if(StringUtils.isNotBlank(fileKeyVO.getShowId())){
            infoVO = fileInfoIntegration.getFileInfoDetail(fileKeyVO.getShowId());
        }else{
            throw new AppException("请传入showId或文件id");
        }
        return infoVO;
    }

    @Override
    @OpApi(funcCode = "fileManager0003", title = "根据MD5获取文件信息", funcType = FuncTypeEnum.query)
    public List<FileInfoVO> getFileInfoByMd5(FileMd5VO fileMd5VO) {
        if(StringUtils.isBlank(fileMd5VO.getFileMd5())){
            throw new AppException("md5不能为空");
        }
        return fileInfoIntegration.getFileInfoByMd5(fileMd5VO.getFileMd5());
    }

    @Override
    @OpApi(funcCode = "fileManager0004", title = "新增文件基础信息", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "fileManager0004",
            operationName = "新增文件基础信息",
            dataType = "fileId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void addFileInfo(FileInfoVO fileInfoVO) {
        fileInfoIntegration.addFileInfo(fileInfoVO);
    }

    @Override
    @OpApi(funcCode = "fileManager0005", title = "修改文件基础信息", funcType = FuncTypeEnum.update)
    @SaveOplog(
            operationType = "fileManager0005",
            operationName = "修改文件基础信息",
            dataType = "fileId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void updateFileInfo(FileInfoVO fileInfoVO) {
        fileInfoIntegration.updateFileInfo(fileInfoVO);
    }

    @Override
    @OpApi(funcCode = "fileManager0006", title = "删除文件基础信息", funcType = FuncTypeEnum.update)
    @SaveOplog(
            operationType = "fileManager0006",
            operationName = "删除文件基础信息",
            dataType = "showId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void deleteFileInfo(FileKeyVO fileKeyVO) {
        if(StringUtils.isNotBlank(fileKeyVO.getShowId())){
            FileInfoVO fileInfoVO=fileInfoIntegration.getFileInfoDetail(fileKeyVO.getShowId());
            fileInfoIntegration.deleteFileInfo(fileInfoVO.getFileId(),fileInfoVO.getTableName(),fileInfoVO.getShowId());
        }else{
            throw new AppException("请传入文件查看id");
        }
    }

    @Override
    @OpApi(funcCode = "fileManager0007", title = "获取文件元数据信息列表", funcType = FuncTypeEnum.query)
    public PageInfo<FileMetadataVO> getFileMetadataList(FileMetadataVO fileMetadataVO) {
        return fileInfoIntegration.getFileMetadataList(fileMetadataVO);
    }

    @Override
    @OpApi(funcCode = "fileManager0008", title = "修改文件元数据信息", funcType = FuncTypeEnum.query)
    public FileMetadataVO updateMetaData(FileMetadataVO fileMetadataVO) {
        return fileInfoIntegration.updateMetaData(fileMetadataVO);
    }

    @Override
    @OpApi(funcCode = "fileManager0009", title = "获取服务大厅文件信息列表", funcType = FuncTypeEnum.query)
    public PageInfo<FileInfoVO> getFileHomeInfo(FileInfoVO fileInfoVO) {
        return  fileInfoIntegration.getFileHomeInfo(fileInfoVO);
    }


    @Override
    @OpApi(funcCode = "fileManager0010", title = "获取授权文件信息列表", funcType = FuncTypeEnum.query)
    public PageInfo<FileInfoVO> getEmpowerFile(FileInfoVO fileInfoVO) {
        Map<String,Object> map = Convert.toMap(String.class,Object.class,fileInfoVO);
        map.forEach((key,val)->{
            if(val instanceof String){
                if(StringUtils.isBlank((String)val)){
                    map.put(key,null);
                }
            }
        });
        fileInfoVO = Convert.convert(FileInfoVO.class,map);
        return fileInfoIntegration.getEmpowerFile(fileInfoVO);
    }
}
